import request from "./../request.js";
import $var from "./../var.js";

let region = [];
let addressText = '';
let addressList = [];
let obj = {
	province_name:'',
	city_name:'',
	county_name:'',
	address:'',
	contact_name:'',
	contact_mobile:''
};
let testList = [
	'顾涛 18521303170 江苏省苏州市张家港市 金港镇紫竹庭院9-1102', //拼多多
	'收货人: 顾涛 手机号码: 18521303170 所在地区: 江苏省苏州市张家港市金港镇 详细地址: 紫竹庭院9-1102', //淘宝
	'姓名：顾涛 地址：江苏苏州市张家港市金港镇 紫竹庭院9-1102', //京东
	'胡海侠[0345] 18400653600 云南省 曲靖市 麒麟区 前北路114号[0345]', //老冉提供
];
//
let devFun = function(fun) {
	for (let i in testList) {
		addressText = '';
		obj = {};
		addressText = testList[i];
		addressList = [];
		process(fun);
	}
}


//获取省市区
let process = function(fun) {

	addressText = addressText.replace(/\s*/g, "");
	addressText = addressText.replace(/:*/g, "");
	addressText = addressText.replace(/：*/g, "");
	addressText = addressText.replace(/收货人*/g, "");
	addressText = addressText.replace(/姓名*/g, "");
	addressText = addressText.replace(/手机号码*/g, "");
	addressText = addressText.replace(/地址*/g, "");
	addressText = addressText.replace(/(所在地区)*/g, "");
	addressText = addressText.replace(/(所在区)*/g, "");
	addressText = addressText.replace(/【*/g, "");
	addressText = addressText.replace(/】*/g, "");
	addressText = addressText.replace(/\[*/g, "");
	addressText = addressText.replace(/\]*/g, "");
	// console.log(addressText);

	addressText = addressText.replace(/:*/g, "");
	addressText = addressText.replace(/:*/g, "");
	addressList = [];
	obj = {};
	addressList.push(addressText);
	let regionIndex = 0;
	let mobileIndex = 0;
	//省市区匹配
	for (let i in region) {
		let row = region[i];
		if (row.class_type == 1) {
			let r = matchRegion(row.class_name, '省');
			if (r !== -1) {
				obj.province_name = row.class_name;
				obj.province_id = row.class_id;
				regionIndex = r + row.class_name.length;
			}

		} else if (row.class_type == 2) {
			let r = matchRegion(row.class_name, '市');
			if (r !== -1) {
				obj.city_name = row.class_name;
				obj.city_id = row.class_id;
				regionIndex = r + row.class_name.length;
			}
		} else if (row.class_type == 3) {
			let r = matchRegion(row.class_name, '区');
			if (r !== -1) {
				obj.county_name = row.class_name;
				obj.county_id = row.class_id;
				regionIndex = r + row.class_name.length;
			}
		}
	}
	// console.log(addressList);
	//手机号匹配
	let addressListTemp = [];
	for (let x in addressList) {
		let item = addressList[x];
		// console.log(item);
		let mobiles = item.match(/1[34578]\d{9}/);
		if (mobiles && mobiles.length > 0) {
			let mobile = mobiles[0];
			mobileIndex = item.indexOf(mobile);
			item = item.replace(mobile, "");
			obj.contact_mobile = mobile;
		}
		if (item) {
			addressListTemp.push(item);
		}
	}
	addressList = addressListTemp;

	if (addressList[0]) {
		let user_name = addressList[0].match(/[\u4e00-\u9fa5]/g);
		if(user_name){
			obj.contact_name = user_name.join('');
		}
	}
	if (addressList[1]) {
		obj.address = addressList[1];
	}
	console.log(obj);
	fun(obj);

}

let matchRegion = function(regionName, regionTag) {
	let addressListTemp = [];
	let splitList = null;
	let indexof = -1;
	for (let x in addressList) {
		let item = addressList[x];
		indexof = item.indexOf(regionName + regionTag);
		if (indexof === -1) {
			indexof = item.indexOf(regionName);
			if (indexof === -1) {
				regionName = regionName.replace(regionTag, '');
				indexof = item.indexOf(regionName);
				if (indexof != -1) {
					splitList = item.split(regionName);
				}
			} else {
				splitList = item.split(regionName);
			}

		} else {
			splitList = item.split(regionName + regionTag);
		}
		if (indexof === -1) {
			addressListTemp.push(item);
		} else {
			addressListTemp = [...addressListTemp, ...splitList];
		}
	}
	addressList = addressListTemp;
	return indexof;

}


let getProvinceList = function() {

}

export default function(areaText,fun, dev = false) {
	addressText = areaText;
	if ($var.region) {
		region = $var.region;
		process(fun)
		//测试
		dev && devFun(fun);
	} else {
		request.$get({
			url: '/common/sys-region/drop-list',
			fun: (resp) => {
				$var.region = resp;
				region = resp;
				process(fun);
				//测试
				dev && devFun(fun);
			}
		});
	}
}
